package it.trekker.tests.online;

import it.trekker.dao.DAOFactory;
import it.trekker.dao.Excursion;
import it.trekker.dao.ExcursionCoordinate;
import it.trekker.dao.IExcursionCoordinateDAO;
import it.trekker.dao.IExcursionDAO;
import it.trekker.dao.ejb.EJB3DAOFactory;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.After;

public class TestCommons {
	
	protected static final Logger logger = Logger.getLogger("TrekkerServer test");
	
	protected DAOFactory daoFactory = DAOFactory.getDAOFactory(EJB3DAOFactory.class.getName());
	protected IExcursionDAO excursionDAO = daoFactory.getExcursionDAO();
	protected IExcursionCoordinateDAO excursionCoordinateDAO = daoFactory.getExcursionCoordinateDAO();
	
	@After
	public void cleanUp() {
		for(Excursion excursion : excursionDAO.findAllExcursions()) {
			excursionDAO.remove(excursion.getId());
		}
	}

	protected Excursion insertAnExcursion(String name) {
		Excursion excursion = new Excursion();
		excursion.setDate(Calendar.getInstance().getTime());
		excursion.setName(name);
		excursion.setSteps(0);
		excursion.setDistance(3200);
		excursion.setMeanPace(12.3212);
		excursion.setTime(7200000);
		excursion.setTotAltitudeDifference(10);
		excursion.setId(excursionDAO.insert(excursion));
		
		return excursion;
	}
	
	protected ExcursionCoordinate insertACoordinate(Excursion excursion, double latitude, double longitude) {
		ExcursionCoordinate coord = new ExcursionCoordinate();
		coord.setExcursion(excursion);
		coord.getCoordinate().setLatitude(latitude);
		coord.getCoordinate().setLongitude(longitude);
		coord.getCoordinate().setAltitude(88);
		coord.setTimestamp(Calendar.getInstance().getTime());
		coord.setId(excursionCoordinateDAO.insert(coord));
		
		return coord;
	}
	
	protected void logExcursions(List<Excursion> list) {
		for(Excursion excursion : list) {
			System.out.println(excursion);
		}
	}
	
}
